//
//  OneKindBettingView.m
//  WangLottery
//
//  Created by 黄修勇 on 12-4-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "OneKindBettingView.h"
#import <QuartzCore/QuartzCore.h>
#import "BellButton.h"

@interface OneKindBettingView (Private)

-(void)setDefault;

-(void)setNumberOfBell:(NSInteger)numOfBell isMustReload:(BOOL)isMust;
@end

@implementation OneKindBettingView


@synthesize minSelectNumber=_minSelectNumber;
@synthesize maxSelectNumber=_maxSelectNumber;
@synthesize numOfBell=_numOfBell;
@synthesize selectNumbers=_selectNumbers;
@synthesize titleString=_titleString;
@synthesize firstNumberIsZero=_firstNumberIsZero;
@synthesize numbersOfSigleLine=_numbersOfSigleLine;


-(void)dealloc{

    [selectContentView release];
    [bellContentView release];
    [selectNums release];
    [selectBell release];
    [titleLabel release];
    [super dealloc];
}

-(id)initWithCoder:(NSCoder *)aDecoder{

    self=[super initWithCoder:aDecoder];
    if (self) {
        [self setDefault];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setDefault];
    }
    return self;
}

-(void)setDefault{

    _firstNumberIsZero=YES;
    _numbersOfSigleLine=7;
    selectNums=[NSMutableArray new];
    selectBell=[NSMutableArray new];
    
    selectContentView=[[UIScrollView alloc]initWithFrame:CGRectMake(15.0f, 15.0f, self.frame.size.width-30.0f, 55.0f)];
    selectContentView.layer.cornerRadius=8.0f;
    selectContentView.layer.backgroundColor=[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.4f].CGColor;
    [self addSubview:selectContentView];
    
    titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10.0f, selectContentView.frame.origin.y+60.0f, self.frame.size.width-30.0f, 20.0f)];
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.textAlignment=UITextAlignmentCenter;
    [self addSubview:titleLabel];
    
    bellContentView=[[UIView alloc]initWithFrame:CGRectMake(15.0f, titleLabel.frame
                                                            .origin.y+titleLabel.frame.size.height+5.0f, self.frame.size.width-30.0f, 100.0f)];
    bellContentView.layer.cornerRadius=8.0f;
    bellContentView.layer.backgroundColor=[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.4].CGColor;
    [self addSubview:bellContentView];
    
}

-(void)setTitleString:(NSString *)titleString{

    titleLabel.text=titleString;
}

-(NSString *)titleString{

    return titleLabel.text;
}

-(void)setNumberOfBell:(NSInteger)numOfBell isMustReload:(BOOL)isMust{

    if (_numOfBell==numOfBell&&isMust==NO) {
        return;
    }
    
    _numOfBell=numOfBell;
    
    for (UIView *view in bellContentView.subviews) {
        [view removeFromSuperview];
    }
    float buttonMargin=(bellContentView.frame.size.width-30.0f-30.0f*_numbersOfSigleLine)/(_numbersOfSigleLine-1);
    NSInteger tempFirstNumber;
    NSInteger tempEndNumber;
    if (self.firstNumberIsZero==YES) {
        tempFirstNumber=0;
        tempEndNumber=numOfBell;
    }else {
        tempFirstNumber=1;
        tempEndNumber=numOfBell+1;
    }
    for (int i=tempFirstNumber; i<tempEndNumber; i++) {
        NSInteger line;
        if (self.firstNumberIsZero) {
            line=i/_numbersOfSigleLine;
        }else {
            line=(i-1)/_numbersOfSigleLine;
        }
        NSInteger tempNum;
        if(self.firstNumberIsZero){
            tempNum=i-line*7;
        }else {
            tempNum=i-line*7-1;
        }
        
        BellButton *btn=[BellButton creatBellWithFrame:CGRectMake(15.0f+tempNum*(30.0f+buttonMargin),10.0f+line*(10.0f+30.0f), 30.0f, 30.0f)];
        btn.number=i;
        btn.tag=i;
        btn.bellColor=BellColorRed;
        btn.bellState=BellStateNormal;
        [btn addTarget:self action:@selector(bellBtnDone:) forControlEvents:UIControlEventTouchUpInside];
        [bellContentView addSubview:btn];
    }
    NSInteger lines=numOfBell/7+1;
    bellContentView.frame=CGRectMake(bellContentView.frame.origin.x, bellContentView.frame.origin.y, bellContentView.frame.size.width, 10.0f+lines*40.0f);
    
}
-(void)setNumOfBell:(NSInteger)numOfBell{

    [self setNumberOfBell:numOfBell isMustReload:NO];
}

-(NSArray *)selectNumbers{

    return [NSArray arrayWithArray:selectNums];
}

-(void)setFirstNumberIsZero:(BOOL)firstNumberIsZero{

    if (self.firstNumberIsZero==firstNumberIsZero) {
        return;
    }
    
    _firstNumberIsZero=firstNumberIsZero;
    [self setNumberOfBell:_numOfBell isMustReload:YES];
}

-(void)setNumbersOfSigleLine:(NSInteger)numbersOfSigleLine{

    if (numbersOfSigleLine==_numbersOfSigleLine) {
        return;
    }
    
    _numbersOfSigleLine=numbersOfSigleLine;
    [self setNumberOfBell:_numOfBell isMustReload:YES];
}

-(void)bellBtnDone:(BellButton *)btn{

    if (btn.bellState==BellStateNormal) {
        [self addSelectNumber:btn.number];
    }else {
        [self removeSelectNumber:btn.number];
    }
    
}

-(BOOL)addSelectNumber:(NSInteger)number{

    NSNumber *num=[NSNumber numberWithInt:number];
    if ([selectNums containsObject:num]) {
        return NO;
    }
    
    for (BellButton *btn in bellContentView.subviews) {
        if (btn.number==number) {
            btn.bellState=BellStateSelct;
        }
    }
    
    BellButton *btn=[BellButton creatBellWithFrame:CGRectMake(15.0f+selectBell.count*(40.0f+10.0f), 55.0f/2-40.0f/2, 40.0f, 40.0f)];
    btn.bellState=BellStateSelct;
    btn.bellColor=BellColorRed;
    [btn addTarget:self action:@selector(bellBtnDone:) forControlEvents:UIControlEventTouchUpInside];
    [selectContentView addSubview:btn];
    [selectBell addObject:btn];
    [selectNums addObject:num];
    [self reloadBellNumber];
    return YES;
}

-(BOOL)removeSelectNumber:(NSInteger)number{

    NSNumber *num=[NSNumber numberWithInt:number];
    if (![selectNums containsObject:num]) {
        return NO;
    }
    [[selectBell lastObject]removeFromSuperview];
    [selectBell removeLastObject];
    for (BellButton *btn in bellContentView.subviews) {
        if (btn.number==number) {
            btn.bellState=BellStateNormal;
            break;
        }
    }
    [selectNums removeObject:num];
    [self reloadBellNumber];
    return YES;
}


-(void)reloadBellNumber{
    
    [selectNums sortUsingSelector:@selector(compare:)];

    for (int i=0; i<selectNums.count; i++) {
        NSInteger number=[[selectNums objectAtIndex:i] intValue];
        BellButton *btn=[selectBell objectAtIndex:i];
        btn.number=number;
    }
    float tempViewWidth=0.0f;
    if (selectNums.count>0) {
        tempViewWidth=30.0f+50.0f*selectNums.count;
    }else {
        tempViewWidth=selectContentView.frame.size.width;
    }
    selectContentView.contentSize=CGSizeMake(tempViewWidth, selectContentView.frame.size.height);
    [selectContentView scrollRectToVisible:CGRectMake(tempViewWidth-50.0f, 0.0f, selectContentView.frame.size.width, selectContentView.frame.size.height) animated:YES];
}

-(void)removeAllSelect{

    
    for (NSNumber *i in selectNums) {
        
        NSInteger index=[i intValue];
        BellButton *btn=(BellButton *)[bellContentView viewWithTag:index];
        [btn setBellState:BellStateNormal];
        
    }
    
     [selectNums removeAllObjects];
    
    for (UIView *v in selectBell) {
        [v removeFromSuperview];
    }
    
    [selectBell removeAllObjects];
    
      selectContentView.contentSize=CGSizeMake(selectContentView.frame.size.width, selectContentView.frame.size.height);

     
}

- (void)randomSelect
{
    [self removeAllSelect];
    
    NSMutableArray *redRandomBall=[[NSMutableArray alloc] init];
    NSInteger typeRedSelectNum=0;
    
    NSInteger maxRedNum=0;
    
    if (_numOfBell==22) {
        
        typeRedSelectNum=5;
        
        maxRedNum=22;

    }
    else if(_numOfBell==30){
        typeRedSelectNum=7;
        
        maxRedNum=30;
        
    }
    

    //机选 随机红球
    for (int i=0; i<typeRedSelectNum; i++) {
        
        int index = arc4random() % maxRedNum + 1;
        
        BOOL mark=YES; // 查找是否生成相同的标记
        for (int j=0; j<[redRandomBall count]; j++) {
            
            int index2=[[redRandomBall objectAtIndex:j] intValue];
            
            if (index2 == index) {
                mark=NO;
                i--;
                break;
            }
        }
        if (mark) [redRandomBall addObject:[NSNumber numberWithInt:index]];
        
    }
    
    for (int  r=0; r<[redRandomBall count]; r++) {
        
        int index=[[redRandomBall objectAtIndex:r] intValue];
        
        [self addSelectNumber:index];
        
    }
    
    [redRandomBall removeAllObjects];
    [redRandomBall release];
    
    [self  reloadBellNumber];

}

@end
